package com.itouch.bracelet.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.R.color;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.itouch.bracelet.adapter.PersonCircleAdapter;
import com.itouch.bracelet.bean.CircleInfo;
import com.itouch.bracelet.bean.CircleResult;
import com.itouch.bracelet.diseaseinfo.Constants;
import com.itouch.util.JsonAndPhpUtil;
import com.itouch.widget.MyListView;
import com.itouch.widget.MyListView.startRefresh;

public class PersonCircleActivity extends Activity{

	private MyListView lv;
	private String userId;
	private String logo;
	private String name;

	private int page=1;
	
	private CircleResult result;	
	
	private ExecutorService es;
	
	private PersonCircleAdapter adapter;
	
	private Handler mHandler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch(msg.what){
			case 0:
				if(page==1){
					adapter=new PersonCircleAdapter(PersonCircleActivity.this, result.getRows());
					lv.setAdapter(adapter);
				}else{
					adapter.updateData(result.getRows());
					adapter.notifyDataSetChanged();
				}
				lv.stopLoadMore();
				if(result.getRows().size()<10){
					lv.setPullLoadEnable(false);
				}else{
					lv.setPullLoadEnable(false);
				}
				lv.stopRefresh();
				break;
			}
		}
		
	};
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		userId=getIntent().getStringExtra("id");
		logo=getIntent().getStringExtra("logo");
		name=getIntent().getStringExtra("name");
		
		es=Executors.newSingleThreadExecutor();
		
		lv = new MyListView(this,new startRefresh() {
			
			@Override
			public void loadData() {
				// TODO Auto-generated method stub
				page=1;
				es.execute(getList);
			}

			@Override
			public void logoClick() {
				// TODO Auto-generated method stub
				//到个人资料页面
				/*Intent in=new Intent();
				in.setClass(mContext, BussinessZdtcDetailInfoActivity.class);
				in.putExtra("memberId", memberId);
				startActivity(in);*/
			}

			@Override
			public void loadMore() {
				// TODO Auto-generated method stub
				page++;
				es.execute(getList);
			}
		},name,logo);
		lv.setDivider(null);
		lv.setCacheColorHint(color.transparent);
		setContentView(lv);
		lv.startCircleAnimation();
		
		adapter=new PersonCircleAdapter(PersonCircleActivity.this, new ArrayList<CircleInfo>());
		lv.setAdapter(adapter);
		
		es.execute(getList);
	}
	
	
	private Runnable getList=new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			List<NameValuePair> lists = new ArrayList<NameValuePair>();
			lists.add(new BasicNameValuePair("rows", "10"));
			lists.add(new BasicNameValuePair("page", page+""));
			lists.add(new BasicNameValuePair("filter_S_id", userId));
			//lists.add(new BasicNameValuePair("filter_S_id", "17"));
			String ss = JsonAndPhpUtil.postHttpData(Constants.URL+"/handchain/mobi/friendCircle/friendInfoById",lists, PersonCircleActivity.this);	
			result=new Gson().fromJson(ss, CircleResult.class);
			Message msg=mHandler.obtainMessage(0);
			msg.sendToTarget();
		}
	};
	
}
